package com.itfetter.search.listen;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.itfetter.api.client.ItemClient;
import com.itfetter.common.utils.BeanUtils;
import com.itfetter.search.domain.dto.ItemDTO;
import com.itfetter.search.domain.po.Item;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.get.GetResult;
import org.springframework.stereotype.Component;
//import top.javatool.canal.client.annotation.CanalTable;
//import top.javatool.canal.client.handler.EntryHandler;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

//@RequiredArgsConstructor
//@CanalTable("item")
//@Component
//public class CanalListener implements EntryHandler<Item> {
//    private final ItemClient itemClient;
//    private final RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.74.128:9200")));
//
//    @Override
//    public void insert(Item item) {
//
//        System.out.println("开始增加数据");
//        ItemDTO itemDTO = BeanUtils.copyBean(item, ItemDTO.class);
//        String doc = JSONUtil.toJsonStr(itemDTO);
//        try {
//            IndexRequest request = new IndexRequest("items").id(String.valueOf(itemDTO.getId()));
//            request.source(doc, XContentType.JSON);
//            esClient.index(request, RequestOptions.DEFAULT);
//            esClient.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @Override
//    public void update(Item before, Item after) {
//        System.out.println("数据库发生修改");
//        ItemDTO beforeDto = BeanUtils.copyBean(before, ItemDTO.class);
//        ItemDTO afterDto = BeanUtils.copyBean(after, ItemDTO.class);
//        UpdateRequest request = null;
//        try {
//            request = new UpdateRequest("items", String.valueOf(after.getId()));
//            String doc = JSONUtil.toJsonStr(afterDto);
//            request.doc(doc, XContentType.JSON);
//            esClient.update(request, RequestOptions.DEFAULT);
//            esClient.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    @Override
//    public void delete(Item item) {
//        System.out.println("开始删除数据");
//        DeleteRequest request = new DeleteRequest("items", String.valueOf(item.getId()));
//        try {
//            esClient.delete(request, RequestOptions.DEFAULT);
//            esClient.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}
